#include <iostream>
#include <functional>

void displayNum(int i)
{
    std::cout << i << "\n";
}

class Foo
{
public:
    Foo(int num)
    {
        _num = num;
    }
    // void printf(int i) const
    void printf(int i)
    {
        _num += i;
        std::cout << "---- " << _num << "\n";
    }

private:
    int _num;
};

struct printNum
{
    void operator()(int i) const
    {
        std::cout << ">>>>>>>>> " << i << "\n";
    }
};

int main(int argc, char const *argv[])
{
    std::function<void(int)> fdisplay = displayNum;
    fdisplay(20);
    auto fdisplay1 = displayNum;
    fdisplay1(19);

    std::function<void()> fdisplay2 = []()
    { displayNum(32); };
    fdisplay2();
    fdisplay2 = std::bind(displayNum, 40);
    fdisplay2();

    Foo f = 600;

    // 存储成员函数及对象的调用
    using std::placeholders::_1;
    std::function<void(int)> fdisplay3 = std::bind(displayNum, _1);
    fdisplay3(60);

    std::function<void(int)> fdisplay4 = std::bind(&Foo::printf, f, _1);
    fdisplay4(4);

    std::function<void(int)> fdisplay5 = std::bind(Foo::printf, &f, _1);
    fdisplay5(90);
    fdisplay5(0);

    std::function<void(int)> fdisplay6 = printNum();
    fdisplay6(76);
    return 0;
}
